<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" size="small" @click="showAddCate">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 分类表格 -->
      <tree-table
        :data="cateList"
        :columns="columns"
        show-index
        border
        :expand-type="false"
        :selection-type="false"
      >
        <template slot="deleted" scope="scope">
          <div>
            <i
              style="font-size:18px;color:lightgreen"
              class="el-icon-success"
              v-if="!scope.row.cat_deleted"
            ></i>
            <i style="font-size:18px;color:red" class="el-icon-error" v-else></i>
          </div>
        </template>
        <template slot="level" scope="scope">
          <div>
            <el-tag size="small" v-if="scope.row.cat_level === 0">一级</el-tag>
            <el-tag size="small" type="success" v-else-if="scope.row.cat_level ===1">二级</el-tag>
            <el-tag size="small" type="warning" v-else>三级</el-tag>
          </div>
        </template>
        <template slot="opt" scope="scope">
          <div>
            <el-button round type="primary" size="mini" class="el-icon-edit" @click="showEditCate(scope.row)">编辑</el-button>
            <el-button round type="danger" size="mini" class="el-icon-delete" @click="deleteCate(scope.row)">删除</el-button>
          </div>
        </template>
      </tree-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <el-dialog title="添加分类" :visible.sync="addDialogVisiable" width="50%" @close="addDialogClose">
      <el-form
        :model="addCateForm"
        ref="addCateFormRef"
        label-width="70px"
      >
        <el-form-item label="分类名称">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            v-model="selectParent"
            :options="parentCateList"
            :props="{ expandTrigger: 'hover' , value: 'cat_id', label: 'cat_name', children: 'children' ,checkStrictly: true}"
            clearable
            @change="selectChange"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisiable = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑分类 弹窗 -->
    <el-dialog title="修改分类" :visible.sync="editDialogVisiable" width="50%">
      <el-form
        :model="editCateForm"
        ref="editCateFormRef"
        label-width="70px"
      >
        <el-form-item label="分类名称">
          <el-input v-model="editCateForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisiable = false">取 消</el-button>
        <el-button type="primary" @click="editCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //商品分类列表查询参数
      queryInfo: {
        pagenum: 1,
        pagesize: 5,
      },
      cateList: [],
      total: 0,
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
          width: "200px",
        },
        {
          label: "是否有效",
          prop: "cat_deleted",
          minWidth: "80px",
          type: "template",
          template: "deleted",
        },
        {
          label: "排序",
          minWidth: "200px",
          prop: "cat_level",
          type: "template",
          template: "level",
        },
        {
          label: "操作",
          minWidth: "200px",
          type: "template",
          template: "opt",
        },
      ],
      //添加分类 是否显示
      addDialogVisiable: false,
      // 添加分类表单
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0,
      },
      //父级分类
      parentCateList: [],
      //选中的父级节点
      selectParent: [],
      //编辑分类弹窗页面 是否显示
      editDialogVisiable:false,
      editCateForm: {
          cat_name: '',
          cat_id: 0,
      }
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200)
        return this.$message.error("获取商品分类列表失败！");
      this.cateList = res.data.result;
      this.total = res.data.total;
      console.log(this.cateList);
    },
    //调整每页显示数量
    handleSizeChange(newPage) {
      this.queryInfo.pagesize = newPage;
      this.getCateList();
    },
    handleCurrentChange(pageNum) {
      this.queryInfo.pagenum = pageNum;
      this.getCateList();
    },
    //显示多级分类 弹窗
    showAddCate() {
      this.addDialogVisiable = true;
      this.getParent();
    },
    //获取多级分类
    async getParent(){
        const { data: res } = await this.$http.get("categories", {
        params: { type: 2 },
      });
      console.log(res);
      if (res.meta.status !== 200)
        return this.$message.error("获取父级分类失败！");
      this.parentCateList = res.data;
    },
    //添加分类
    async addCate(){
        console.log(this.addCateForm);
        const {data : res} = await this.$http.post('categories', this.addCateForm);
        if(res.meta.status !== 201) this.$message.error('添加分类失败！');
        console.log(res);
        this.addDialogVisiable = false;
        this.$message.success('添加分类成功！');
        this.getCateList();
    },
    //选中多级分类时
    selectChange(value){
        console.log(value);
        if(this.selectParent.length > 0){
            this.addCateForm.cat_pid = this.selectParent[this.selectParent.length -1];
            this.addCateForm.cat_level = this.selectParent.length;
        }else{
            this.addCateForm.cat_level = 0;
            this.addCateForm.cat_pid = 0;
        }
    },
    // 添加分类窗口关闭事件
    addDialogClose(){
        console.log(this);
        this.addCateForm.cat_pid = 0,
        this.addCateForm.cat_level = 0,
        this.addCateForm.cat_name = '',
        this.selectParent = []
    },
    // 显示编辑分类弹窗
    showEditCate(cateinfo){
        this.editCateForm.cat_id = cateinfo.cat_id;
        this.editDialogVisiable = true;
    },
    // 保存编辑分类
    async editCate(){
        console.log(this.editCateForm);
        const {data : res} = await this.$http.put(`categories/${this.editCateForm.cat_id}`,{cat_name:this.editCateForm.cat_name});
        if(res.meta.status !== 200) return this.$message.error('更新分类失败！');
        this.editDialogVisiable = false;
        this.$message.success('更新分类信息成功！');
        this.editCateForm.cat_name = '';
        this.editCateForm.cat_id = 0;
        this.getCateList();
    },
    //删除分类
    async deleteCate(cateinfo){
        var result = await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(res => res)
        if(result == 'cancel') return this.$message.info('用户取消了删除操作');
        const {data : res} = await this.$http.delete(`categories/${cateinfo.cat_id}`);
        if(res.meta.status !== 200) return this.$message.error('删除分类失败！'); 
        this.$message.success('已删除分类！');
        this.getCateList();
    }
  },
};
</script>

<style lang="less" scoped>
.el-card {
  margin-top: 15px;
}
.zk-table {
  margin-top: 15px;
}
</style>